package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.admin.TbUser;
import com.dongdongshop.pojo.cart.Cart;
import com.dongdongshop.pojo.order.TbOrder;
import com.dongdongshop.service.order.OrderService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {
    @Reference
    private OrderService orderService;
    private Logger logger = LoggerFactory.getLogger(OrderController.class);
    @RequestMapping("insertOrder")
    @ResponseBody
    public Result insertOrder(Long addressId,String pay,String order){
        try {
            List<Cart> cart = JSON.parseArray(order, Cart.class);
            String orderParentId = orderService.insertOrder(addressId,pay,cart);
            return Result.ok().setData(orderParentId);

        }catch (Exception e){
            logger.error("添加失败",e);
            return Result.fail();
        }
    }
    //待发货页面
    @RequestMapping("orderSendShow")
    public String orderSendShow(){
        return "home-order-send";
    }
    //带评论页面
    @RequestMapping("orderEvaluate")
    public String orderEvaluate(){
        return "home-order-evaluate";
    }
    //查询待发货信息
    @RequestMapping("listOrderSend")
    @ResponseBody
    public Result listOrderSend(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            List<TbOrder> orderList = orderService.listOrder("3",user.getUsername());
            return Result.ok().setData(orderList);
        }catch (Exception e){
            logger.error("查询失败",e);
            return Result.fail();
        }
    }
    @RequestMapping("listOrderEvaluate")
    @ResponseBody
    public Result listOrderEvaluate(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            List<TbOrder> orderList = orderService.listOrder("3",user.getUsername());
            return Result.ok().setData(orderList);
        }catch (Exception e){
            logger.error("查询失败",e);
            return Result.fail();
        }
    }
}
